<?php

namespace frontend\controllers;

use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use Yii;
use backend\models\Category;
use backend\models\Product;
use frontend\models\Cart;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

class SuperController extends Controller {

	/**
	 * 通用成功跳转  
	 * @param unknown $url 成功后跳转的URL
	 * @param number $sec 自动跳转秒数
	 * @return Ambigous <string, string>
	 */
	public function success($url = [], $sec = 2) {
		$this->layout = "layout2";
		$url = empty($url) ? ['index'] : $url;
		$url = \yii\helpers\Url::toRoute($url);
		return $this->render('../base/msg', ['gotoUrl' => $url, 'sec' => $sec]);
	}

	/**

	 * 通用错误跳转

	 * @param string $msg 错误提示信息

	 * @param number $sec

	 * @return Ambigous <string, string>

	 */
	public function error($msg = '', $url = [], $sec = 2) {
		$this->layout = "layout2";
		if (empty($url)) {
			return $this->render('../base/msg', ['errorMessage' => $msg, 'sec' => $sec]);
		} else {
			$url = empty($url) ? ['index'] : $url;
			$url = \yii\helpers\Url::toRoute($url);
			return $this->render('../base/msg', ['gotoUrl' => $url, 'sec' => $sec, 'errorMessage' => $msg]);
		}
	}

	public function beforeAction($action) {
		//layout
		$this->layout = 'layout2';
		$view = YII::$app->view;
		//categoryList
		if (parent::beforeAction($action)) {
			//获取带有二级列表的分类列表
			$categoryList2 = $this->getCategoryList2();
			$view->params['categoryList2'] = $categoryList2;
			//productList 热销，最新，推荐栏
			$productList = $this->getProductList();
			$view->params['productList'] = $productList;
			//Cart
			if ($this->isGust()) {
				$view->params['cartTotal'] = 0;
				$view->params['cartList'] = array();
				$view->params['cartCount'] = 0;
			} else {
				$this->setCartList(\Yii::$app->user->id);
			}
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/**
	 * 返回二级分类数组
	 * @param 
	 * @return string
	 */
	public function getCategoryList2() {
		$categoryList = Category::find()->where(['parentId' => 0])->all();
		$categoryList = ArrayHelper::toArray($categoryList);
		$categoryList = ArrayHelper::index($categoryList, 'categoryId');
		$categoryList2;
		foreach ($categoryList as $categoryKey => $categoryUnit) {
			$secondList = Category::find()->where(['parentId' => $categoryUnit['categoryId']])->all();
			$secondList = ArrayHelper::toArray($secondList);
			$secondList = ArrayHelper::index($secondList, 'categoryId');
//			foreach ($secondList as $secondKey => $secondListValue) {
				$categoryList2[$categoryKey] = array($categoryUnit['title'],$secondList);
//			}
		}
		return $categoryList2;
	}

	/**
	 * 返回热销，最新，推荐的商品数组
	 * @param $id int
	 * @return Array
	 */
	public function getProductList() {
		$tui = Product::find()->where('ison = "0" and ishot = "0"')->orderby('saleprice desc')->limit(4)->all();
		$productList['tui'] = $tui;
		$hot = Product::find()->where('ison = "0" and ishot = "0"')->orderby('number')->limit(4)->all();
		$productList['hot'] = $hot;
		$new = Product::find()->where('ison = "0" and ishot = "0"')->orderby('price asc')->limit(4)->all();
		$productList['new'] = $new;
		return $productList;
	}

	/**
	 * 已登录返回1,未登录返回0
	 * @param 无
	 * @return bool
	 */
	public function isGust() {
		return \Yii::$app->user->isGuest;
	}

	/**
	 * 把cart数据放到view里,已登录才能使用
	 * @param $userId 已登录的用户ID
	 * @return null
	 */
	public function setCartList($userId) {
		$view = YII::$app->view;
		$cartModel = Cart::findAll(['userId' => $userId]);
		$count = count($cartModel);
//		echo "<pre>";
//		var_dump($cartModel);
//		echo "</pre>";
//		exit;
		$cartList = ArrayHelper::toArray($cartModel);
		$cartTotal = 0;
		//购物车没有商品
		if ($count == 0) {
			$view->params['cartTotal'] = 0;
			$view->params['cartList'] = array();
			$view->params['cartCount'] = 0;
		} else {
			for ($i = 0; $i < $count; $i++) {
				$productModel = Product::findOne($cartList[$i]['productId']);
				$cartList[$i]['productTitle'] = $productModel->title;
				$cartList[$i]['productCover'] = $productModel->cover;
				$cartList[$i]['productTotal'] = $cartList[$i]['price'] * $cartList[$i]['productnum'];
				$cartTotal += $cartList[$i]['productTotal'];
			}
			$cartList = ArrayHelper::index($cartList, 'cartId');
			$view->params['cartTotal'] = $cartTotal;
			$view->params['cartList'] = $cartList;
			$view->params['cartCount'] = $count;
		}
	}

}
